<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <title>生态圈系统-优惠券池</title>
  <link rel="icon" th:href="@{/images/favicon.ico}" th:mce_href="@{/images/favicon.ico}" type="image/x-icon">
  <link rel="shortcut icon" th:href="@{/images/favicon.ico}" th:mce_href="@{/images/favicon.ico}" type="image/x-icon">

  <!-- CSS  -->
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link href="plugins/bootstrap-4.0.0/css/bootstrap.min.css" th:href="@{/plugins/bootstrap-4.0.0/css/bootstrap.min.css}" type="text/css" rel="stylesheet"/>
  <link href="css/materialize.css" th:href="@{/css/materialize.css}" type="text/css" rel="stylesheet" media="screen,projection"/>
  <link href="plugins/perfect-scrollbar/perfect-scrollbar.css" th:href="@{/plugins/perfect-scrollbar/perfect-scrollbar.css}" type="text/css" rel="stylesheet" media="screen,projection"/>
  <link href="plugins/bootstrap-datepicker/css/bootstrap-datepicker.css" th:href="@{/plugins/bootstrap-datepicker/css/bootstrap-datepicker.css}" type="text/css" rel="stylesheet"/>
  <link href="plugins/bootstrap-sweetalert/sweetalert.css" th:href="@{/plugins/bootstrap-sweetalert/sweetalert.css}" type="text/css" rel="stylesheet"/>
  <link href="css/style.css" th:href="@{/css/style.css}" type="text/css" rel="stylesheet"/>
</head>
<body>
  <header>
    <nav class="" role="navigation" style="background-color: #f4f6f9;">
      <div class="nav-wrapper">
        <ul class="right hide-on-med-and-down">
          <!--
          <li><a class="blue-text waves-effect dropdown-trigger" href="#!" data-target="dropdown1" style="vertical-align: top;"><span th:if="${session.user!=null}" th:text="${session.user.username}">admin</span><i class="material-icons right">arrow_drop_down</i></a></li>
           -->
          <li><a class="grey-text waves-effect dropdown-trigger" href="#!" data-target="dropdown1" style="vertical-align: top;"><span sec:authentication="name">admin</span><i class="material-icons right">arrow_drop_down</i></a></li>
          <ul id="dropdown1" class="dropdown-content">
            <li><a href="#modal3" class="waves-effect grey-text modal-trigger">修改密码</a></li>
            <li><a th:href="@{/admin/logout}" class="waves-effect grey-text">退出登录</a></li>
          </ul>
        </ul>

        <a href="#" data-target="slide-out" class="sidenav-trigger"><i class="material-icons">menu</i></a>
      </div>
    </nav>
  </header>

  <div th:replace="common/aside_admin :: aside"></div>

  <main class="" style="height: calc(100% - 48px); background-color: #f4f6f9;">
    <div class="row">
      <h6 style="margin: 0 0 0 15px;" id="page-title" active-menu="coupon-pool-menu">优惠券池</h6>
    </div>

    <div class="row white" style="margin: 0 15px; height: inherit;">


      <div class="col s12" style="height: inherit;"> <!-- Note that "m8 l9" was added -->
        <div class="">
          <form id="search">
            <div class="row" style="margin-bottom: 0;">
              <div class="input-field col col-md-3 s2 blue-text">
                <select name="activityId">
                  <option value="" selected>权益</option>
                  <option th:each="a : ${activities}" th:value="${a.id}"  th:text="${a.title}" th:selected="${a.id} == ${search.activityId}"></option>
                </select>
              </div>
              <div class="input-field col col-md-2 s2">
                <input type="text" name="couponCode" id="coupon_code" th:value="${search.couponCode}">
                <label for="coupon_code">验证码</label>
              </div>
              <div class="input-field col col-md-1 s3">
                <a th:href="'javascript:search();'" class="waves-effect waves-light btn-floating search-btn"><i class="material-icons right">search</i></a>
              </div>
              <div class="input-field col col-md-6 s6">
                <a class="waves-effect waves-light btn light-blue darken-1 right white-text modal-trigger" href="#modal2" style="margin-right: 30px;"><i class="material-icons right"></i>生成优惠券</a>
                <a class="waves-effect waves-light btn light-blue darken-1 right white-text modal-trigger" href="#modal1" style="margin-right: 10px;"><i class="material-icons right"></i>导入优惠券</a>
                <a class="waves-effect waves-light right" href="/kangchenjunga/upload-template/coupon-import-template.xlsx" style="top: 0.5rem; margin-right: 10px; line-height: 1.5rem; color: rgba(0, 0, 0, 0.54);"><i class="material-icons" style="position: relative; top: 0.5rem; color: rgba(0, 0, 0, 0.54);">file_download</i>下载模板</a>
              </div>
            </div>
          </form>
        </div>

        <div style="padding-top: 0; height: inherit;">

          <div>
            <table>
              <thead>
                <tr>
                  <!-- <th width="5%">
	                  <label>
	                    <input type="checkbox" id="select_all"/>
	                    <span></span>
	                  </label>
                  </th> -->
                  <th width="30%">权益</th>
                  <th width="25%">验证码</th>
                  <th width="25%">密码</th>
                  <th width="15%">有效天数</th>
                </tr>
              </thead>
            </table>
          </div>

          <div id="table_body_div" style="height: calc(100% - 88px); position: relative;">
            <div>
	            <table class="highlight">
	              <thead>
	                <tr>
	                  <!-- <th class="no-padding" width="5%"></th> -->
	                  <th class="no-padding" width="30%"></th>
	                  <th class="no-padding" width="25%"></th>
	                  <th class="no-padding" width="25%"></th>
	                  <th class="no-padding" width="15%"></th>
	                </tr>
	              </thead>
	              <tbody>
	                <tr th:each="c : ${coupons}">
	                  <!-- <td>
                      <label>
	                      <input type="checkbox"/>
	                      <span></span>
	                    </label>
	                  </td> -->
	                  <td th:text="${c.activityTitle}" th:title="${c.activityTitle}">中秋大闸蟹</td>
	                  <td th:text="${c.couponCode}" th:title="${c.couponCode}"></td>
	                  <td th:text="${c.secretCode}" th:title="${c.secretCode}">2018-09-01</td>
	                  <td th:text="${c.termByMinute / 1440}" th:title="${c.termByMinute / 1440}">2018-09-30</td>
	                </tr>
	              </tbody>
	            </table>
            </div>
          </div>
          <ul class="pagination" th:if="${coupons.totalPages} > 1">
            <!-- <li class="disabled"><a href="#!"><i class="material-icons">chevron_left</i></a></li>
            <li class="active"><a href="#!">1</a></li>
            <li class="waves-effect"><a href="#!">2</a></li>
            <li class="waves-effect"><a href="#!">3</a></li>
            <li class="waves-effect"><a href="#!">4</a></li>
            <li class="waves-effect"><a href="#!">5</a></li>
            <li class="waves-effect"><a href="#!"><i class="material-icons">chevron_right</i></a></li> -->
          </ul>

        </div>

      </div>

  	</div>
  </main>

  <!-- <footer class="white page-footer teal">
    <div class="footer-copyright">
      <div class="black-text container">
      Made by <a class="brown-text text-lighten-3" href="http://materializecss.com">Materialize</a>
      </div>
    </div>
  </footer> -->

  <!-- Modal Structure -->

  <div id="modal1" class="modal modal-fixed-footer" style="width: 500px; height: 400px;">
    <div class="modal-header">
      <h6>导入优惠券</h6>
      <a class="btn-flat modal-close"><i class="material-icons">close</i></a>
    </div>
    <div class="modal-content" style="border: unset; border-radius: 0;">
      <div class="row" style="width: 100%;">
        <div class="col s12">
          <div class="row">
            <div class="col s12 input-field">
              <select name="activityId">
                <option value="" disabled selected>请选择</option>
                <option th:each="a : ${activities}" th:value="${a.id}"  th:text="${a.title}"></option>
              </select>
              <label>权益</label>
            </div>
          </div>
          <div class="row">
            <form id="uploadForm" class="col s12 file-field input-field">
              <div class="btn light-blue darken-1">
                <span>选择文件</span>
                <input type="file" name="file" class="validate" required="required" accept="application/vnd.ms-excel, application/vnd.ms-works, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
              </div>
              <div class="file-path-wrapper">
                <input class="file-path" type="text">
              </div>
            </form>
          </div>
          <input type="reset" style="display:none;" />
        </div>
      </div>
    </div>
    <div class="modal-footer">
      <a href="#!" class="modal-close waves-effect waves-light btn-flat" style="margin-right: 10px;">取消</a>
      <button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action" style="margin-right: 10px;" onclick="upload()">导入</button>
    </div>
  </div>
  
  <div id="modal2" class="modal modal-fixed-footer" style="width: 500px; height: 400px;">
    <div class="modal-header">
      <h6>生成优惠券</h6>
      <a class="btn-flat modal-close"><i class="material-icons">close</i></a>
    </div>
    <div class="modal-content" style="border: unset; border-radius: 0;">
      <div class="row" style="width: 100%;">
        <div class="col s12">
          <div class="row">
            <div class="col s12 input-field">
              <select name="activityId">
                <option value="" disabled selected>请选择</option>
                <option th:each="a : ${activities}" th:value="${a.id}"  th:text="${a.title}"></option>
              </select>
              <label>权益</label>
            </div>
          </div>
          <div class="row">
            <div class="col s6 input-field">
              <input type="number" name="quantity" id="quantity" class="validate" required min="1">
              <label for="quantity"><font color="red">*</font>数量</label>
              <span class="helper-text" data-error="wrong" data-success="right"></span>
            </div>
          </div>
          <div class="row">
            <div class="col s6 input-field">
              <input type="number" name="termByDay" id="termByDay" class="validate" required min="1">
              <label for="termByDay"><font color="red">*</font>有效天数</label>
              <span class="helper-text" data-error="wrong" data-success="right"></span>
            </div>
          </div>
          <input type="reset" style="display:none;" />
        </div>
      </div>
    </div>
    <div class="modal-footer">
      <a href="#!" class="modal-close waves-effect waves-light btn-flat" style="margin-right: 10px;">取消</a>
      <button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action" style="margin-right: 10px;" onclick="generate()">确定</button>
    </div>
  </div>

  <div th:replace="common/update_pwd :: #modal3"></div>

  <!--  Scripts-->
  <script src="js/jquery-3.3.1.min.js" th:src="@{/js/jquery-3.3.1.min.js}"></script>
  <script src="js/materialize.js" th:src="@{/js/materialize.js}"></script>
  <script src="plugins/perfect-scrollbar/perfect-scrollbar.min.js" th:src="@{/plugins/perfect-scrollbar/perfect-scrollbar.min.js}"></script>
  <script src="plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js" th:src="@{/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js}"></script>
  <script src="plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js" th:src="@{/plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js}" charset="UTF-8"></script>
  <script src="plugins/bootstrap-sweetalert/sweetalert.min.js" th:src="@{/plugins/bootstrap-sweetalert/sweetalert.min.js}"></script>
  <script src="js/common.js" th:src="@{/js/common.js}"></script>
  <script src="js/jquery.page.js" th:src="@{/js/jquery.page.js}"></script>
  <script th:inline="javascript">
    $(".pagination").createPage({
      pageCount: [[${coupons.totalPages}]],
      current: [[${coupons.number}]] + 1,
      backFn:function(p){
        window.location.href = "pools?page=" + (p - 1) + "&" + formParamsSerialize($("#search").find('input, select'));
      }
    });

    function search() {
      console.log($("#search").serialize());
      window.location.href = "pools?" + formParamsSerialize($("#search").find('input, select'));
    }

    function upload() {
    	var activityId = $('#modal1 select[name="activityId"]').val();
      if(activityId == null || activityId == "") {
        swal("请选择权益!", {
          icon: "error",
        });
        return;
      }
    	var form = new FormData(document.getElementById("uploadForm"));
      $.ajax({
        url: "/kangchenjunga/api/v1/activities/" + activityId + "/coupons/import",
        data: form,
        processData: false,
        contentType: false,
        type: "PUT",
        success: function(data, textStatus) {
          console.log(data);
          swal({
            title: "",
            text: "导入成功!",
            icon: "success",
            //timer: 2000,
          });
          window.location.reload();
          $("#modal1 input[type=reset]").trigger("click");
          $('#modal1').modal('close');
        },
        error: function (data, textStatus, errorThrown) {
          //console.log(textStatus);
          swal({
            title: "导入失败!",
            text: data.responseJSON.message,
            icon: "error",
            //timer: 2000,
          });
          M.updateTextFields();
        },
        statusCode: {400: function() {
        	swal({
            title: "",
            text: "导入失败!未选择文件!",
            icon: "error",
            //timer: 2000,
          });
          M.updateTextFields();
        }}
      });

    }
    
    function generate() {
      var activityId = $('#modal2 select[name="activityId"]').val();
      if(activityId == null || activityId == "") {
        swal("请选择权益!", {
          icon: "error",
        });
        return;
      }
      var input_elems = $('#modal2 .modal-content input, #modal2 .modal-content textarea');
      input_elems.each(function(){
        M.validate_field($(this));
      });
      var invalid_input_elems = $('#modal2 .modal-content input.invalid, #modal2 .modal-content textarea.invalid');
      if(invalid_input_elems.length > 0) {
        return false;
      }
      $.ajax({
        url: "/kangchenjunga/api/v1/coupons/services/generate/" + activityId,
        data: "amount=" + $("#modal2 input[name='quantity']").val() + "&termByDay=" + $("#modal2 input[name='termByDay']").val(),
        type: "POST",
        success: function(data, textStatus) {
          console.log(data);
          swal({
            title: "",
            text: "生成优惠券成功!",
            icon: "success",
            //timer: 2000,
          });
          window.location.reload();
          $("#modal1 input[type=reset]").trigger("click");
          $('#modal1').modal('close');
        },
        error: function (data, textStatus, errorThrown) {
          //console.log(textStatus);
          swal({
            title: "生成优惠券失败!",
            text: data.responseJSON.message,
            icon: "error",
            //timer: 2000,
          });
          M.updateTextFields();
        }
      });

    }
  </script>
  </body>
</html>
